package com.cyan.app.workflow.receiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;

import com.cyan.app.workflow.service.MainService;

/**
 * Created by GivenTang on 2015/7/28.
 */
public class MainReceiver extends BroadcastReceiver {
    private static final String TAG = "MainReceiver";

    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent.getAction().equals("android.intent.action.BOOT_COMPLETED")) {

            MainService.start(context);
        } else if (intent.getAction().equals("android.intent.action.ACTION_SHUTDOWN")) {

            MainService.stop(context);
        } else if (intent.getAction().equals(ConnectivityManager.CONNECTIVITY_ACTION)) {
            ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo activeNetInfo = cm.getActiveNetworkInfo();

            if (activeNetInfo != null && activeNetInfo.isAvailable()) {
                MainService.start(context);
            } else {
                MainService.stop(context);
            }
        } else if (intent.getAction().equals("android.intent.action.USER_PRESENT")) {
            MainService.start(context);
        }
    }
}
